print("generate protoolkit export function file")

import re
import os
import sys

protkincludePath = os.environ[sys.argv[1]] + "\protoolkit\includes"


print("protkincludePath: ",protkincludePath)

hfile = open('protkFun.h','w')
cppfile = open('protkFun.cpp','w')

Cfun = re.compile(r"""
                    extern
                    \s+
                    [^{},\(\)\s]+
                    \s+
                    [^{}*,\(\)\s]+
                    \s*
                    \(
                    [^(]*
                    \)
                    \s*
                    ;
                    """, re.X|re.M)

contfun = 0;

def seachfile(filepath,outfile):
    afile = open(filepath,'r')
    pattern = re.compile(r'Pro')
    #match = Cfun.search(afile.read())
    #if match:
        # 使用Match获得分组信息
        #print(match.group())
    #    outfile.write(match.group())
    li = Cfun.findall(afile.read())
    for s in li:
        global contfun
        contfun+=1
        outfile.write('//'+str(contfun)+'\n')
        outfile.write('/*'+s+'*/\n')
        exprtFun(s,outfile)

def walk_dir(dir,fileh,filecpp):
    fileh.write('//'+dir + '\n')
    filecpp.write('#include "'+fileh.name+ '"\n')
    for root, dirs, files in os.walk(dir):
        for name in files:
            #print(name)
            patternl = re.compile(r'ProMech')
            match1 = patternl.match(name)
            if match1:
                print("skip: ",name)
                continue;#skip ProMechanica 

            fileh.write('#include "'+name+ '"\n')
            filehpath = os.path.join(root,name)
            filecpp.write('//'+filehpath + '\n')
            seachfile(filehpath,filecpp)
        for name in dirs:
            print(os.path.join(name))
            #fileinfo.write('  ' + os.path.join(root,name) + '\n')

inoglist = ['ProAsynchronousEventLoopInterrupt'
            ,'ProAsynchronousEventLoop'
            ,'ProTermFuncSet'
            ,'ProEngineerDisconnect'
            ,'ProToolkitUnicodeencodingDisable'
            ,'ProEngineerConnectionStart'
            ,'ProEngineerStatusGet'
            ,'ProEngineerConnectWS'
            ,'ProAsynchronousMain'
            ,'ProEngineerConnect'
            ,'ProEngineerConnectIdExtract'
            ]

skipli = []

def exprtFun(funstring,outfile):
    funcnamere = re.compile(r"(\S+)(\s*\()")
    matchafn = funcnamere.search(funstring)
    caglist = '';
    funcname = '';
    if matchafn:
        funcname = matchafn.group(1)
        outfile.write('/*funcname:' + funcname +'*/\n')
        
    
    ret = re.search(r'\.\.\.', funstring)
    if ret:
        print('ing :'+funcname+'\n')
        global skipli
        skipli.append(funcname)
        return

    if funcname in inoglist:
        return
        
    aglistre = re.compile(r"\(([^)]*)\)")
    match = aglistre.search(funstring)
    if match:
        aglist = match.group(0)
        outfile.write('/*aglist:' + aglist +'*/\n')
        epag0 = re.sub(r"PRO_CONST_ARG|const\s+|unsigned",r'',aglist)
        epag1 = re.sub(r"\(*\s*(\S+)([\s\*]+)(\S+)\s*([,\)])",r'\3\4',epag0)
        epag2 = re.sub(r"\s+|\(*void|\[\d*\]",r'',epag1)
        caglist = epag2
        outfile.write('/*aglistCall:' + caglist +'*/\n')
       
    e1 = re.sub(r"(\S+)(\s*\()",r'Py\1\2',funstring)
    es = re.sub(r"(extern)(\s+)",r'extern "C" __declspec(dllexport)\2',e1)
    e4 = re.sub(r"(;)",r'',es)
    outfile.write(e4+'\n')
    outfile.write('{return '+funcname+'('+caglist+';}'+'\n')

walk_dir(protkincludePath,hfile,cppfile)

cppfile.write('//skipFun'+str(skipli)+'\n')
